package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpBp3Mapper;
import com.ruoyi.system.domain.SpBp3;
import com.ruoyi.system.service.ISpBp3Service;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 编排3Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-12
 */
@Service
public class SpBp3ServiceImpl implements ISpBp3Service 
{
    @Autowired
    private SpBp3Mapper spBp3Mapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询编排3
     * 
     * @param cj3Id 编排3主键
     * @return 编排3
     */
    @Override
    public SpBp3 selectSpBp3ByCj3Id(Long cj3Id)
    {
        return spBp3Mapper.selectSpBp3ByCj3Id(cj3Id);
    }

    /**
     * 查询编排3列表
     * 
     * @param spBp3 编排3
     * @return 编排3
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpBp3> selectSpBp3List(SpBp3 spBp3)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spBp3.setDatascope(datascope);
        if(datascope==4){
            spBp3.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spBp3.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spBp3.setCheckHuiyuanId(0l);
            }

        }
        return spBp3Mapper.selectSpBp3List(spBp3);
    }

    @Override
    public List<SpBp3> selectSpBp3ListWeb(SpBp3 spBp3)
    {
        return spBp3Mapper.selectSpBp3List(spBp3);
    }

    /**
     * 新增编排3
     * 
     * @param spBp3 编排3
     * @return 结果
     */
    @Override
    public int insertSpBp3(SpBp3 spBp3)
    {
        return spBp3Mapper.insertSpBp3(spBp3);
    }

    /**
     * 修改编排3
     * 
     * @param spBp3 编排3
     * @return 结果
     */
    @Override
    public int updateSpBp3(SpBp3 spBp3)
    {
        return spBp3Mapper.updateSpBp3(spBp3);
    }

    /**
     * 批量删除编排3
     * 
     * @param cj3Ids 需要删除的编排3主键
     * @return 结果
     */
    @Override
    public int deleteSpBp3ByCj3Ids(Long[] cj3Ids)
    {
        return spBp3Mapper.deleteSpBp3ByCj3Ids(cj3Ids);
    }

    /**
     * 删除编排3信息
     * 
     * @param cj3Id 编排3主键
     * @return 结果
     */
    @Override
    public int deleteSpBp3ByCj3Id(Long cj3Id)
    {
        return spBp3Mapper.deleteSpBp3ByCj3Id(cj3Id);
    }
}
